package com.huluobo.a2111douyindemo.login;


import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2111douyindemo.MainActivity;
import com.huluobo.a2111douyindemo.R;
import com.huluobo.a2111douyindemo.databinding.ActivityLoginBinding;
import com.huluobo.a2111douyindemo.register.RegisterActivity;
import com.huluobo.mvvm_base.mvvm.base.view.BaseMVVMActivity;
import com.huluobo.mvvm_base.mvvm.bean.Money;
import com.huluobo.mvvm_base.mvvm.bean.UserBeanResult;
import com.huluobo.mvvm_base.mvvm.db.AppDBUtils;
import com.huluobo.mvvm_base.mvvm.db.MoneyDao;
import com.huluobo.mvvm_base.mvvm.net.Const;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

/**
 * ActivityLoginBinding 对应xml文件,会根据xml文件名生成一个对应的dataBinding布局(驼峰命名法)
 * 比如: activity_login.xml 会对应生成ActivityLoginBinding的一个类
 */
public class LoginActivity extends BaseMVVMActivity<ActivityLoginBinding, LoginViewModel> {

    @Override
    protected void initData() {
        //观察ViewModel中LiveData的值是否发生变化,如果发生变化,代表成功或者失败已经赋值
        viewModel.loginSuccess.observe(this, userBeanResult -> {
            if (userBeanResult.getCode() == 200) {
                ToastUtils.showLong("欢迎回来:" + userBeanResult.getData().getUsername());
                SPUtils.getInstance().put(Const.SP_TOKEN, userBeanResult.getData().getToken());
                SPUtils.getInstance().put(Const.SP_USERNAME, userBeanResult.getData().getUsername());
                //通过数据库保存用户
                AppDBUtils.getInstance().getUserDao().insertUserInfo(userBeanResult.getData());
                Money money = new Money(0, userBeanResult.getData().getUsername(), 10000L);
                AppDBUtils.getInstance().getMoneyDao().insertUserMoney(money);
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
//                //登录环信
//                EMClient.getInstance().login(userBeanResult.getData().getUsername(),
//                        userBeanResult.getData().getPassword(), new EMCallBack() {
//                            @Override
//                            public void onSuccess() {
//                                Log.i("TagA", "登录环信成功");
//                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//                                startActivity(intent);
//                                finish();
//                            }
//
//                            @Override
//                            public void onError(int code, String error) {
//                                Log.i("TagA", "登录环信失败:" + error);
//                            }
//                        });



            } else {
                ToastUtils.showLong(userBeanResult.getMessage());
            }
        });

        viewModel.loginFailed.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong("登录失败:" + s);
            }
        });
    }

    @Override
    protected void initView() {
        //点击登录按钮触发登录
        dataBinding.loginToLogin.setOnClickListener(v -> {
            login();
        });

        //注册文字的点击事件
        dataBinding.loginToRegister.setOnClickListener(v -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        });

        dataBinding.loginLogout.setOnClickListener(v -> {
            EMClient.getInstance().logout(true);
        });
    }

    //登录的方法
    private void login() {
        String usernameString = dataBinding.loginUsername.getText().toString().trim();
        String passwordString = dataBinding.loginPassword.getText().toString().trim();
        if (TextUtils.isEmpty(usernameString) || TextUtils.isEmpty(passwordString)) {
            ToastUtils.showLong("请输入用户名和密码");
            return;
        }
        //验证了用户名密码都不为空,验证合法
        viewModel.login(usernameString, passwordString);
    }

    @Override
    protected Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }
}